﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;
using BUS;

using DTO;


namespace FastDeal.Control
{
    public partial class DangKyMoi : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                lber.Visible = false;
                Label1.Visible = false;
                lberuser.Visible = false;
                lberemail.Visible = false;
                lbercmnd.Visible = false;
                lberCapcha.Visible = false;
            }
        }

        TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();

        private bool KiemTraUserDangKy(string username)
        {
            if (tkBUS.KiemTraUserDangKy(username))
            {
                return false;
            }
            else
                return true;
        }

        // kiểm tra user
        private bool KiemTraUserDangKy()
        {
            if (tkBUS.KiemTraUserDangKy(txtUserName.Text) == false)
            {
                return false;
            }
            else
                return true;
        }

        // kiểm tra email
        private bool KiemTraEmailDangKy()
        {
            if (tkBUS.KiemTraEmailDangKy(txtEmail.Text) == false)
            {
                return false;
            }
            else
                return true;
        }

        // kiểm tra cmnd có trùng ko
        public bool KiemTraCMNDDangKy()
        {
            if (tkBUS.KiemTraCMNDDangKy(txtCMND.Text) == false)
            {
                return false;
            }
            else
                return true;
        }
        public string MaHoaMatKhau(string password)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            Byte[] hashBytes = encoding.GetBytes(password);
            // Compute the SHA-1 hash
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            Byte[] cryptPassword = sha1.ComputeHash(hashBytes);
            return BitConverter.ToString(cryptPassword).Substring(0, 8);
        }

        private bool DangKy1(int maloaitv, string ghichu)
        {
            TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();
            ThongTinTaiKhoan_BUS tttkBUS = new ThongTinTaiKhoan_BUS();
            TaiKhoan tk = new TaiKhoan();
            ThongTinTaiKhoan tttk = new ThongTinTaiKhoan();
            List<TaiKhoan> listtk = new List<TaiKhoan>();

            if (KiemTraUserDangKy())
            {
                if (KiemTraEmailDangKy())
                {
                    if (KiemTraCMNDDangKy())
                    {
                        string maXacNhan = txtCaptcha.Text;
                        CaptchaControl1.ValidateCaptcha(maXacNhan);
                        if (CaptchaControl1.UserValidated == true)
                        {
                            tk.UserName = txtUserName.Text;
                            tk.Password = MaHoaMatKhau(txtPassword.Text);
                            tk.UserName = txtUserName.Text;
                            tk.MaLoaiThanhVien = maloaitv;
                            tk.TrangThai = 1;
                            tk.HoatDongCuoiCung = Convert.ToDateTime(DateTime.Now.ToString());
                            tk.isDelete = 0;
                            tk.Email = txtEmail.Text;
                            tkBUS.Them_TaiKhoan(tk);      

                            tttk.UserID = tk.UserID;
                            tttk.HoTen = txtHoTen.Text;
                            tttk.Nickname = txtNickName.Text;
                            tttk.DiaChi = txtDiaChi.Text;
                            tttk.DienThoai = txtDienThoai.Text;
                            tttk.CMND = txtCMND.Text;
                            tttk.GhiChu = ghichu;
                            tttk.isDelete = 0;

                            if (tttkBUS.Them_ThongTinTaiKhoan(tttk))
                            {
                                Label1.Text = "Bạn đã đăng kí thành công!";
                                Label1.Visible = true;
                            }
                        }
                        else
                        {
                            lberCapcha.Text = "Mã Xác Nhận Chưa Đúng";
                            lberCapcha.Visible = true;
                            return false;
                        }
                    }
                    else
                    {
                        lbercmnd.Text = "CMND đã tồn tại.";
                        lbercmnd.Visible = true;
                        return false;
                    }
                }
                else
                {
                    lberemail.Text = "Email đã tồn tại.";
                    lberemail.Visible = true;
                    return false;
                }
            }
            else
            {
                lberuser.Text = "Username đã tồn tại.";
                lberuser.Visible = true;
                return false;
            }
            return true;
        }

        private void DangNhap()
        {
            TaiKhoan_BUS db = new TaiKhoan_BUS();
            TaiKhoan taikhoan = new TaiKhoan();
            taikhoan = db.LayThongTin_TaiKhoan(txtUserName.Text);

            Session["DaDangNhap"] = true;
            Session["LoaiThanhVien"] = taikhoan.MaLoaiThanhVien;
            Session["Username"] = taikhoan.UserName;
            Session["UserID"] = taikhoan.UserID;
            switch (int.Parse(taikhoan.MaLoaiThanhVien.ToString()))
            {
                case 2:
                    Response.Redirect("~/User/Default.aspx");
                    break;
                case 3:
                    Response.Redirect("~/DoanhNghiep/Default.aspx");
                    break;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            lber.Visible = false;
            Label1.Visible = false;
            lberuser.Visible = false;
            lberemail.Visible = false;
            lbercmnd.Visible = false;
            lberCapcha.Visible = false;

            TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();
            ThongTinTaiKhoan_BUS tttkBUS = new ThongTinTaiKhoan_BUS();
            TaiKhoan tk = new TaiKhoan();
            ThongTinTaiKhoan tttk = new ThongTinTaiKhoan();
            List<TaiKhoan> listtk = new List<TaiKhoan>();

            // kiểm tra chọn loại tài khoản hay chưa, nếu chưa return
            if (rbMember.Checked)
            {
                tk.MaLoaiThanhVien = 2;
                tttk.GhiChu = "Tài khoản member";
                if (DangKy1(tk.MaLoaiThanhVien, tttk.GhiChu))
                {
                    DangNhap();
                    Label1.Visible = true;
                    txtUserName.Text = String.Empty;
                    txtEmail.Text = String.Empty;
                    txtHoTen.Text = String.Empty;
                    txtNickName.Text = String.Empty; ;
                    txtCMND.Text = String.Empty;
                    txtDienThoai.Text = String.Empty; ;
                    txtDiaChi.Text = String.Empty;
                }
            }
            else
                if (rbDoanhNghiep.Checked)
                {
                    tk.MaLoaiThanhVien = 3;
                    tttk.GhiChu = "Tài khoản doanh nghiệp";
                    if (DangKy1(tk.MaLoaiThanhVien, tttk.GhiChu))
                    {
                        DangNhap();
                        Label1.Visible = true;
                        txtUserName.Text = String.Empty;
                        txtEmail.Text = String.Empty;
                        txtHoTen.Text = String.Empty;
                        txtNickName.Text = String.Empty; ;
                        txtCMND.Text = String.Empty;
                        txtDienThoai.Text = String.Empty; ;
                        txtDiaChi.Text = String.Empty;
                    }
                }
                else
                {
                    lber.Text = "Chọn 1 trong 2 loại tài khoản.";
                    lber.Visible = true;
                    return;
                }


        }

        protected void imbReLoad_Click(object sender, ImageClickEventArgs e)
        {
            CaptchaControl1.Focus();
        }
    }
}